package com.czxy.controller;

import com.czxy.domain.BaseDict;
import com.czxy.domain.Customer;
import com.czxy.service.BaseDictService;
import com.czxy.service.CustomerService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {


    @Resource
    private CustomerService customerService;

    @Resource
    private BaseDictService baseDictService;

    @RequestMapping("/findAll")
    public String findAll(Model model){
        List<Customer> list = customerService.findAll();
        model.addAttribute("list", list);
        return "customer/list";
    }


    @RequestMapping("/add")
    public String findAll(Customer customer){
        customerService.insert(customer);
        return "redirect:/customer/findAll.action";
    }


    @RequestMapping("/delete")
    public String delete(Customer customer){
        customerService.delete(customer);
        return "redirect:/customer/findAll.action";
    }


    public  String editUI(Customer customer,Model model){

        Customer custById = customerService.findById(customer);

        List<BaseDict> all = baseDictService.findAll();

        List<BaseDict> all2 = baseDictService.findAll();

        model.addAttribute("allcustSource",all);
        model.addAttribute("allcustSource1",all2);
        model.addAttribute("customer",custById);

        return "customer/edit.jsp";

    }


    public  String edit(Customer customer,Model model){

        boolean rel = customerService.update(customer);

        if (rel){

            return "redirect:/customer/findAll.action";

        }else{
            List<BaseDict> all = baseDictService.findAll();

            List<BaseDict> all2 = baseDictService.findAll();

            model.addAttribute("allcustSource",all);
            model.addAttribute("allcustSource1",all2);
            model.addAttribute("customer",customer);


            model.addAttribute("editMsg","修改失败");


            return "customer/edit";

        }

    }





















}
